package com.abbas.smartCity.ui;

import android.text.TextUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.abbas.smartCity.App;
import com.abbas.smartCity.R;
import com.abbas.smartCity.base.BaseFragment;
import com.abbas.smartCity.bean.UserInfoBean;
import com.bumptech.glide.Glide;

/**
 * 个人中心
 */
public class MineFragment extends BaseFragment {

    private ImageView imageView;
    private TextView nickName;
    private TextView userInfoTv;
    private TextView changePasswordTv;
    private TextView orderTv;
    private TextView advTv;
    private Button logoutBt;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void initView() {
        imageView = (ImageView) rootView.findViewById(R.id.imageView);
        nickName = (TextView) rootView.findViewById(R.id.nick_name);
        userInfoTv = (TextView) rootView.findViewById(R.id.user_info_tv);
        changePasswordTv = (TextView) rootView.findViewById(R.id.change_password_tv);
        orderTv = (TextView) rootView.findViewById(R.id.order_tv);
        advTv = (TextView) rootView.findViewById(R.id.adv_tv);
        logoutBt = (Button) rootView.findViewById(R.id.logout_bt);
    }

    @Override
    protected void initData() {
        //用户数据，在登录时已经拿到
        final UserInfoBean.UserBean user = App.getUser();

        //如果用户没有头像，则设置为此图片
        if (!TextUtils.equals("",user.getAvatar())){
            Glide.with(requireContext())
                    .load(App.getBaseUrl() + user.getAvatar())
                    .error(R.drawable.avatar)
                    .into(imageView);
        }
        if (!TextUtils.equals("",user.getNickName())){
            nickName.setText(user.getNickName());
        }

    }

    @Override
    protected void initEvent() {
        //用户信息点击
        userInfoTv.setOnClickListener(view -> startActivity(UserInfoActivity.class));
        //更改密码点击
        changePasswordTv.setOnClickListener(view -> startActivity(ChangePasswordActivity.class));
        //订单列表点击
        orderTv.setOnClickListener(view -> {});
        //意见反馈点击
        advTv.setOnClickListener(view -> startActivity(AdviseActivity.class));

        //登出
        logoutBt.setOnClickListener(view -> {
            //清除用户信息
            App.setUser(null);
            //App.getSharedPreferences().edit().clear().apply();无需此操作
            //跳转值登录页面
            startActivity(LoginActivity.class);
            //结束主页
            requireActivity().finish();
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        //在页面恢复时重新加载此页面数据，防止用户数据修改之后显示的用户数据不一致
        initData();
    }
}